package com.landking.test_bmob;


import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.landking.R;
import com.landking.base.BaseActivity;
import com.landking.db.table.LK_PIC_M;
import com.landking.db.table.LK_USER_M;
import com.landking.utils.FunUtil;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 快速入门
 *
 * @ClassName: BmobActivity
 * @date 2014-5-20 下午4:10:29
 */
public class BmobActivity extends BaseActivity implements OnClickListener {

    Button btn_add, btn_delete, btn_update, btn_query;

    private String objectId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmob_test);
        // 初始化View
        initView();
        initListener();
    }

    private void initView() {
        btn_add = (Button) findViewById(R.id.btn_add);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        btn_update = (Button) findViewById(R.id.btn_update);
        btn_query = (Button) findViewById(R.id.btn_query);
    }

    private void initListener() {
        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        btn_query.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btn_add) {
            createPerson();
        } else if (v == btn_delete) {
            deletePersonByObjectId();
        } else if (v == btn_update) {
            updatePersonByObjectId();
        } else if (v == btn_query) {
            queryPersonByObjectId();
        }
    }

    /**
     * 创建一条person数据 createPersonData
     *
     * @throws
     * @Title: createPersonData
     */
    private void createPerson() {
        /*final LK_USER_M p2 = new LK_USER_M();
        p2.setUserid("10000");
        p2.setUsername("李一诺");
        p2.setPassword("lucky");
        p2.setAddress("北京东城区建国门内大街7号长安大剧院");
        p2.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    Toast.makeText(BmobActivity.this, "添加数据成功，返回objectId为：" + objectId, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(BmobActivity.this, "创建数据失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });*/
        final LK_PIC_M p2 = new LK_PIC_M();
        p2.setPictype("");//类型
        p2.setPictitle("");// 标题
        p2.setPicpath("");
        p2.setPicname("");
        p2.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    Toast.makeText(BmobActivity.this, "添加数据成功，返回objectId为：" + objectId, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(BmobActivity.this, "创建数据失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    /**
     * 更新指定objectId的person数据
     *
     * @return void
     * @throws
     */
    private void updatePersonByObjectId() {
        //更新Person表里面id为42fbfb8800的数据，address内容更新为“北京朝阳”
        final LK_USER_M p2 = new LK_USER_M();
        p2.setAddress("北京朝阳");
        p2.setPassword("A1234567");
        p2.update("42fbfb8800", new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Toast.makeText(BmobActivity.this, "更新成功:" + p2.getUpdatedAt(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(BmobActivity.this, "更新失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

        });

    }

    /**
     * 删除指定ObjectId的person数据 deletePersonByObjectId
     *
     * @return void
     * @throws
     * @Title: deletePersonByObjectId
     */
    private void deletePersonByObjectId() {
        final LK_USER_M p2 = new LK_USER_M();
        p2.setObjectId("42fbfb8800");
        p2.delete(new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Toast.makeText(BmobActivity.this, "删除成功:" + p2.getUpdatedAt(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(BmobActivity.this, "删除失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

        });
    }

    /**
     * 查询指定ObjectId的person数据
     * queryPerson
     *
     * @throws
     * @Title: queryPerson
     */
    private void queryPersonByObjectId() {
        //查找Person表里面id为42fbfb8800的数据
        BmobQuery<LK_USER_M> bmobQuery = new BmobQuery<LK_USER_M>();
        bmobQuery.getObject("42fbfb8800", new QueryListener<LK_USER_M>() {
            @Override
            public void done(LK_USER_M object, BmobException e) {
                if (e == null) {
                    Toast.makeText(BmobActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(BmobActivity.this, "查询失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

